/**
* 版权所有:版权所有(C) 2020，卓信科技
* 系统名称:IFC-管理系统
* 文件名称:FileHanldeController.java
* 模块名称:文件上传
* 创建日期:2020年5月19日
* 作        者:Baisong
* 内容摘要:文件上传
*/
package com.zx.emanage.sysmanage.web;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.zx.emanage.util.gen.ResultBean;
import com.zx.emanage.util.gen.ResultHelper;
import com.zx.emanage.util.gen.SysUtil;
import com.zx.platform.syscontext.PlatformGlobalVar;
import com.zx.sframe.util.file.GlobalFileUtil;
/**
 * 文件公共处理类
 * @author gx
 *
 */
@Controller
public class FileHanldeController 
{
	private static Logger logger = LoggerFactory.getLogger(FileHanldeController.class);
	@Autowired
	private CommonsMultipartResolver multipartResolver;

	/**
	 * 将文件保存后，返回文件保存的服务器相对路径，如果同时上传多个文件用，分隔文件名称
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/util/fileuploadsave.do")
	@ResponseBody
	public ResultBean<String> handleFormUpload(HttpServletRequest request,
			HttpSession session, String host, String port) 
	{
		String result = "";
		// 判断
		if (this.multipartResolver != null
				&& this.multipartResolver.isMultipart(request)) 
		{
			// 判断
			if (request instanceof MultipartHttpServletRequest) 
			{

				MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;

				Map<String, MultipartFile> fileMap = mRequest.getFileMap();
				// 循环
				for (Map.Entry<String, MultipartFile> entity : fileMap
						.entrySet()) 
				{
					MultipartFile mf = entity.getValue();
					/*long fileSize = mf.getSize();
					// 判断
					if (fileSize > 60 * 1024 * 1024)
					 {
					    result =  "maxsizeerror";  
					}*/
					String srcFileName = mf.getOriginalFilename();
					String postfix = "";
					// 判断
					if (srcFileName.lastIndexOf(".") > -1)
					{
						postfix = srcFileName.substring(srcFileName.lastIndexOf(".")+1).toLowerCase();
						// 判断
						if("exe".equals(postfix) ||"cmd".equals(postfix) || "bat".equals(postfix))
						{
						    result =  "filetypeerror";
						}
					}
					try 
					{
						String path = GlobalFileUtil.saveUploadFile(mf.getInputStream(), null, mf.getOriginalFilename());
                       /* // 生成缩略图2016-10-27
						String isDebug = PlatformGlobalVar.SYS_PROPERTIES.get("isCompressImg");
						// 判断
						if ("true".equals(isDebug))
						{
							SysUtil.asyncCompress(path);
						}*/
						result = result + path +",";
					} 
					catch (IOException e) 
					{
						logger.error(e.getMessage());
						result =   "error";
					}
				}
			}
		}
		// 判断
		if(result.endsWith(","))
		{
		    result = result.substring(0, result.length()-1);
		}
		
		// String httpType = request.getScheme();// request.getScheme()
		//return new ModelAndView("redirect:http://"+PlatformGlobalVar.SYS_PROPERTIES.get("remoteServer")+"/WMS/resources/html/ajaxfileUpload.html?filepath="+result);
		//判断是否成功
		if(result.endsWith("error"))
		{
			return ResultHelper.getError(result);
		}
		else 
		{
			return ResultHelper.getSuccess(result);
		}
	}
}
